#@layout()

#define css()
<link href="#(CPATH)/static/components/jquery-cropper/cropper.css" rel="stylesheet">
<link href="#(CPATH)/static/components/jquery-file-upload/css/jquery.fileupload.css" rel="stylesheet">
<style>
    #uploader {
        height: 230px;
    }

    .myPanel {
        font-size: 25px;
        color: #ccc;
        text-align: center;
        padding-top: 60px;
    }

    .upload-main {
        background-color: #ddd;
        width: 100%;
        height: 370px;
    }

    .preview-content {
        display: flex;
        flex-direction: column;
        height: 100%;
        justify-content: space-between;
    }

    .preview-content img {
        width: 100%;
    }

    .preview-big {
        width: 170px;
        height: 170px;
    }

    .preview-second {
        width: 100px;
        height: 100px;
    }

    .preview-small {
        width: 50px;
        height: 50px;
    }

    .img-preview {
        float: left;
        margin-bottom: .5rem;
        margin-right: .5rem;
        overflow: hidden;
    }

    .upload-main > img, .img-preview > img {
        max-width: 100%;
    }

    .cropper-container,
    .img-preview {
        background-color: #ddd;
        text-align: center;
    }

    .cropper-container {
        margin-bottom: 1rem;
        max-height: 497px;
        min-height: 200px;
        width: 100% !important;
    }
</style>
#end

#define script()
<script src="#(CPATH)/static//components/jquery-cropper/cropper.js"></script><!-- Cropper.js is required -->
<script src="#(CPATH)/static//components/jquery-cropper/jquery-cropper.js"></script>
<script src="#(CPATH)/static/components/jquery-file-upload/js/vendor/jquery.ui.widget.js"></script>
<script src="#(CPATH)/static/components/jquery-file-upload/js/jquery.iframe-transport.js"></script>
<script src="#(CPATH)/static/components/jquery-file-upload/js/jquery.fileupload.js"></script>
<script>



    function selectFile() {
        var categoryId = $("#category").val();
        $('#cfile').fileupload({
            dropZone: $('#uploader'),
            url: '#(CPATH)/commons/attachment/upload?categoryId=' + categoryId,
            sequentialUploads: true,
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $(".myPanel").text("上传进度：" + progress + "%")
            },
            done: function (e, data) {

                $(".myPanel").text("或者拖动文件到这里进行上传...");

                if (data.result.state == "ok") {
                    toastr.success("文件上传成功...")

                    if (data.result.cropEnable) {
                        initCropper(data)
                    }
                } else {
                    toastr.error(data.result.message)
                }
            }
        });
    }

    var cropperImage;
    function initCropper(data) {
        $('#uploadedImg').attr('src', data.result.src);
        $('#path').val(data.result.src);
        $('#attachmentId').val(data.result.attachmentId);

        $(".cropper").show();
        $("#uploader").height("530px")
        $(".myPanel").hide();


        if (cropperImage) {
            cropperImage.cropper('destroy');
        }

        cropperImage = $('#uploadedImg');

        cropperImage.cropper({
            // aspectRatio: 16 / 9,
            // viewMode:3,
            preview: '.img-preview',
            crop: function (event) {
                updateSelect(event.detail);
            }
        });
    }


    function updateSelect(data) {
        $('#x').val(data.x);
        $('#y').val(data.y);
        $('#w').val(data.width);
        $('#h').val(data.height);
        // $('#zoom').val(data.h);
    }

    function refresh() {
        location.replace(location.href);
    }

    $('.aspect-ratio').on('click', function () {
        cropperImage.cropper('setAspectRatio', $(this).attr('data-aspect-ratio'));
    })

</script>
#end

#define content()

<section class="content-header">
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-6">

                <div class="row mb-2">
                    <div class="col-sm-12">
                        <h1>
                            附件上传
                            <small data-toggle="tooltip" title="" data-placement="right"
                                   data-trigger="hover"><i class="nav-icon far fa-question-circle"></i></small>
                            <small> 首页 / 附件 / 附件上传</small>
                        </h1>
                    </div>
                </div>

            </div>

        </div>

    </div><!-- /.container-fluid -->
</section>

<section class="content">

    <form action="#(CPATH)/commons/attachment/doSaveImage" class="autoAjaxSubmit" method="post"
          data-ok-function="refresh">

        <input type="hidden" name="attachmentId" id="attachmentId">
        <input type="hidden" name="path" id="path">
        <input type="hidden" name="x" id="x">
        <input type="hidden" name="y" id="y">
        <input type="hidden" name="w" id="w">
        <input type="hidden" name="h" id="h">
        <input type="hidden" name="zoom" id="zoom">


        <div class="container-fluid">
            <div class="card card-outline card-primary">
                <div class="card-body">
                    <div id="uploader" onclick="selectFile()">
                            <span class="btn btn-block btn-primary fileinput-button" style="width: 120px">
                                <i class="fas fa-plus"></i>
                                <span>选择文件...</span>
                                <input id="cfile" type="file" name="files[]" multiple>
                            </span>

                        <div class="myPanel">
                            或者拖动文件到这里进行上传...
                        </div>

                        <div class="row cropper" style="display: none;padding-top: 20px">
                            <div class="col-sm-9">
                                <div class="upload-main">
                                    <img src="" id="uploadedImg"/>
                                </div>
                            </div>
                            <div class="col-sm-3">
                                <div class="preview-content">
                                    <div class="img-preview preview-big"></div>
                                    <div class="img-preview preview-second"></div>
                                    <div class="img-preview preview-small"></div>
                                </div>
                            </div>

                            <div class="col-12 row">
                                <div class="col-sm-6">
                                    剪切比例：
                                    <a class="mr-3 aspect-ratio" href="javascript:;" data-aspect-ratio="1">1:1</a>
                                    <a class="mr-3 aspect-ratio" href="javascript:;"
                                       data-aspect-ratio="1.7777777777777777">16:9</a>
                                    <a class="mr-3 aspect-ratio" href="javascript:;"
                                       data-aspect-ratio="1.3333333333333333">4:3</a>
                                    <a class="mr-3 aspect-ratio" href="javascript:;"
                                       data-aspect-ratio="0.6666666666666666">2:3</a>
                                    <a class="mr-3 aspect-ratio" href="javascript:;" data-aspect-ratio="NaN">自定义</a>
                                </div>
                                <div class="col-sm-6">
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <button type="submit" class="btn btn-primary float-right mr-2">
                                                <i class="fa fa-fw fa-check"></i> 确定
                                            </button>
                                            <button type="button" onclick="refresh()"
                                                    class="btn btn-outline-secondary float-right mr-2">
                                                <i class="fa fa-fw fa-times"></i> 不剪切
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                    <div style="position: absolute; left: 150px; top:20px">
                        <select class="form-control" id="category" name="attachment.categoryId" onchange="selectFile()">
                            <option value="">上传至分类...</option>
                            #for(item : categories ??)
                            <option value="#(item.id ??)">#(item.title ??)</option>
                            #end
                        </select>
                    </div>

                    <div style="position: absolute; left: 300px; top:30px">
                        <a href="#(CPATH)/admin/attachment/category">分类管理</a>
                    </div>

                </div>
            </div>
        </div>
        </div>
    </form>
</section>

#end
